package tree;

import java.util.ArrayDeque;

public class AVLTree {


    public static void main(String[] args) {
        AVLTree sortTree = new AVLTree();

        sortTree.add(3);
        sortTree.add(2);
        sortTree.add(1);
       sortTree.add(4);
        sortTree.add(5);
         sortTree.add(6);
        sortTree.add(7);
        sortTree.add(10);
        sortTree.add(9);
        sortTree.add(8);
        System.out.println("-------------");
    }

    void sout(AVLTreeNode head) {
        if (head == null) {
            return;
        }
        System.out.println(head.bf);
        sout(head.Left);
        sout(head.right);
    }

    public AVLTreeNode head;
    /**
    * @param  o 要加入的元素
    * */
    public void add(int o) {
        /**
         * 如果输的根节点为空，直接插入根节点中
         * **/
        if (this.head == null) {
            this.head = new AVLTreeNode();
            this.head.bf = 0;
            this.head.data = o;
        } else {
            boolean isTilt = add(o, this.head);
        }
    }

    /**
     * @param o 要加入的元素
     * @param parent 父亲节点
     * @return  taller 是否增加了新的一层
     * */
    private boolean add(int o, AVLTreeNode parent) throws RuntimeException {
        //默认没有增加新的一层，接受递归结果，如果子节点增了孙节点，并且对层数产生改变，它的值将会为true
        boolean taller = false;
        //二叉搜索树性质，往左搜索
        if (o < parent.data) {
            //到了叶子层
            if (parent.Left == null) {
                AVLTreeNode node = new AVLTreeNode(o);
                parent.Left = node;
                node.parent = parent;
                //因为是往左搜索，如果右孩子为空的同时，增加左孩子，必定这个父节点的左子树的层数加一
                if (parent.right != null) {
                    taller = false;
                }
                taller = true;
            } else {
                //非叶子层，进入深层次搜索
                taller = add(o, parent.Left);
            }
            //获得新增节点对层数的影响的结果，增加了一层
            if (taller) {
                //判断新增节点后父节点的平衡因子，在上面使用左孩子递归，所以新节点必定是左孩子的孩子，但尚未修改父节点的平衡因子
                switch (parent.bf) {
                    case 1:
                        //因为是左孩子的层数加一，所以当父节点的bf为1时，新增之后就为2了，需要平衡处理，这里已经是最小不平衡树，终止冒泡，下面同样。计算新增后的平衡因子
                        leftBalance(parent);
                       return  false;
                    case 0:
                        parent.bf = 1;
                        break;
                    case -1:
                        parent.bf = 0;
                        break;
                    default:break;
                }
            }
        } else if (o > parent.data) {
            if (parent.right == null) {
                AVLTreeNode node = new AVLTreeNode(o);
                parent.right = node;
                node.parent = parent;
                if (parent.Left != null) {
                    taller = false;
                }
                taller = true;
            } else {
                taller = add(o, parent.right);
            }
            if (taller) {
                switch (parent.bf) {
                    case 1:
                        parent.bf = 0;
                        break;
                    case 0:
                        parent.bf = -1;
                        break;
                    case -1:
                        rightBalance(parent);
                        return false;
                    default:break;
                }
            }
        } else {
            throw new RuntimeException();
        }
        return taller;
    }


    AVLTreeNode find(int target) {
        ArrayDeque<AVLTreeNode> deque = new ArrayDeque<>();
        if (head.data == target) {
            return head;
        } else {
            if (head.right != null) {
                deque.push(head.right);
            }
            if (head.Left != null) {
                deque.push(head.Left);
            }
            while (!deque.isEmpty()) {
                AVLTreeNode treeNode = deque.pop();
                if (treeNode.data == target) {
                    return treeNode;
                }
                if (treeNode.right != null) {
                    deque.push(treeNode.right);
                }
                if (treeNode.Left != null) {
                    deque.push(treeNode.Left);
                }
            }
            return null;
        }
    }

    void rightRotate(AVLTreeNode target) {
        System.out.println("右旋，target:" + target.data);
        AVLTreeNode node = target.Left;
        if (target.parent != null) {
            if (target.parent.Left == target) {
                target.parent.Left = node;
            } else {
                target.parent.right = node;
            }
        }
        node.parent = target.parent;
        if (target == head) {
            head = node;
        }
        target.Left = node.right;
        node.right = target;
        target.parent = node;
    }

    void leftRotate(AVLTreeNode target) {
        System.out.println("左旋，target:" + target.data);
        AVLTreeNode node = target.right;
        if (target.parent != null) {
            if (target.parent.Left == target) {
                target.parent.Left = node;
            } else {
                target.parent.right = node;
            }
        }
        if (target == head) {
            head = node;
        }
        node.parent = target.parent;
        target.right = node.Left;
        node.Left = target;
        target.parent = node;
    }

    void leftBalance(AVLTreeNode target) {
        AVLTreeNode node = target.Left;
        switch (node.bf) {
            case 1:
                target.bf = target.Left.bf = 0;
                rightRotate(target);
                break;
            case -1:
                switch (node.right.bf) {
                    case 0:
                        node.bf = 0;
                        target.bf = 0;
                        break;
                    case 1:
                        node.bf = 0;
                        target.bf = -1;
                        break;
                    case -1:
                        node.bf = 1;
                        target.bf = 0;
                        break;
                    default:
                        throw new RuntimeException("平衡因子出现错误");
                }
                node.right.bf = 0;
                leftRotate(node);
                rightRotate(target);
                break;
            default:
                throw new RuntimeException("平衡因子出现错误");
        }
    }

    void rightBalance(AVLTreeNode target) {
        AVLTreeNode node = target.right;
        switch (node.bf) {
            case 1:
                switch (node.Left.bf) {
                    case 1:
                        node.bf = -1;
                        target.bf = 0;
                        break;
                    case 0:
                        node.bf = 0;
                        target.bf = 0;
                        break;
                    case -1:
                        node.bf = 0;
                        target.bf = 1;
                        break;
                    default:
                        throw new RuntimeException("平衡因子出现错误");
                }
                node.Left.bf = 0;
                rightRotate(node);
                leftRotate(target);
                break;
            case -1:
                target.bf = 0;
                node.bf = 0;
                leftRotate(target);
                break;
            default:
                throw new RuntimeException("平衡因子出现错误");
        }
    }

    public void remove(int target){
        if (this.head==null){
            throw  new RuntimeException("空树!!!!");
        }
        remove(target,this.head);
    }

    private   boolean remove(int target,AVLTreeNode parent) {
        boolean taller = false;
        return taller;
    }

}

class AVLTreeNode {

    public AVLTreeNode Left;

    public AVLTreeNode right;

    public int data;

    //平衡因子
    public int bf;

    public AVLTreeNode parent;

    public AVLTreeNode() {
        this.bf = 0;
    }

    public AVLTreeNode(int data) {
        this.data = data;
        this.bf = 0;
    }
}

